package com.digitalbonestudios.slugpug.tv;

import java.util.Random;

public class World {
    static final int WORLD_WIDTH = 12;
    static final int WORLD_HEIGHT = 6;
    static final int SCORE_INCREMENT = 10;
    static final float TICK_INITIAL = 0.5f;
    static final float TICK_DECREMENT = 0.05f;

    public Dog dog;
    public DogTreat treat;
    public DogPoop poop;
    public boolean gameOver = false;
    public int score = 0;

    boolean fields[][] = new boolean[WORLD_WIDTH][WORLD_HEIGHT];
    Random random = new Random();
    float tickTime = 0;
    static float tick = TICK_INITIAL;

    public World() {
        dog = new Dog();
        placeTreat();
        placeDogPoop();
    }

    private void placeTreat() {
        for (int x = 0; x < WORLD_WIDTH; x++) {
            for (int y = 0; y < WORLD_HEIGHT; y++) {
                fields[x][y] = false;
            }
        }

        int len = dog.parts.size();
        for (int i = 0; i < len; i++) {
            BodyPart part = dog.parts.get(i);
            fields[part.x][part.y] = true;
        }

        int treatX = random.nextInt(WORLD_WIDTH);
        int treatY = random.nextInt(WORLD_HEIGHT);
        while (true) {
            if (fields[treatX][treatY] == false)
                break;
            treatX += 1;
            if (treatX >= WORLD_WIDTH) {
            	treatX = 0;
            	treatY += 1;
                if (treatY >= WORLD_HEIGHT) {
                	treatY = 0;
                }
            }
        }
        
        treat = new DogTreat(treatX, treatY, random.nextInt(4));
        fields[treatX][treatY] = true;
        
        //As a test block out the 2 bottom right cells 
        // in case we want to move the pause/sound icons
        fields[WORLD_WIDTH -1][WORLD_HEIGHT -1] = true;
        fields[WORLD_WIDTH -2][WORLD_HEIGHT -2] = true;
        
    }
    
    private void placeDogPoop() {
        int poopX = random.nextInt(WORLD_WIDTH);
        int poopY = random.nextInt(WORLD_HEIGHT);
        
        if (poopX >= WORLD_WIDTH)
        	poopX = WORLD_WIDTH - 1;
        if (poopY >= WORLD_HEIGHT)
        	poopY = WORLD_HEIGHT - 1;
        
        //Log.v("Poop", "x: " + poopX + " y: " + poopY);
        
        while (true) {
        	if (poopX >= WORLD_WIDTH || poopY >= WORLD_HEIGHT)
        		break;
        	
            if (fields[poopX][poopY] == false) {
            	break;
            }
            
            poopX += 1;
            if (poopX >= WORLD_WIDTH) {
            	poopX = 0;
            	poopY += 1;
                if (poopX >= WORLD_HEIGHT) {
                	poopY = 0;
                }
            }
            
            //Log.v("Poop", "x: " + poopX + " y: " + poopY);
        }
        
        poop = new DogPoop(poopX, poopY);
    }

    public void update(float deltaTime) {
        if (gameOver)
            return;

        tickTime += deltaTime;

        while (tickTime > tick) {
            tickTime -= tick;
            dog.advance();
            if (dog.checkBitten() || dog.checkSteppedInPoop(poop.x, poop.y)) {
                gameOver = true;
                tick = TICK_INITIAL;
                
                if (Settings.soundEnabled && dog.checkSteppedInPoop(poop.x, poop.y))
                	Assets.playSound(Assets.stinky);
                
                return;
            }

            BodyPart head = dog.parts.get(0);
            
            if (head.x == treat.x && head.y == treat.y) {
                score += SCORE_INCREMENT;
                dog.eat();
                if (Settings.soundEnabled)
                	Assets.playSound(Assets.bark);
                
                if (dog.parts.size() == WORLD_WIDTH * WORLD_HEIGHT) {
                    gameOver = true;
                    return;
                } else {
                    placeTreat();
                    placeDogPoop();
                }

                if (score % 100 == 0 && tick - TICK_DECREMENT > 0) {
                    tick -= TICK_DECREMENT;
                }
            }
        }
    }
}

